package com.backend.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import com.backend.entity.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {

    //根据用户名查询用户
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(String username);

    //插入创建用户
    @Insert("INSERT INTO user (username, password) VALUES (#{username}, #{password})")
    void add(String username, String password);

    //根据id查询用户
    @Select("SELECT * FROM user WHERE id = #{userId}")
    User findById(Integer userId);

    //根据用户名修改用户信息
    @Update("update user set username= #{usernameAfter} where username = #{usernameBefore}")
    void updateByUsername(String usernameBefore,String usernameAfter);

    //查询用户名是否存在
    @Select("SELECT COUNT(*) AS user_count FROM user WHERE username = #{username};")
    int usernameExist(String username);

    //修改用户密码
    @Update("UPDATE user SET password = #{newPassword} WHERE id = #{id}")
    void updatePwd(Integer id, String newPassword);

    //管理员查看全部的用户
    @Select("SELECT * FROM user limit #{pageNumber},#{pageSize};")
    List<User> listAll(Integer pageNumber, Integer pageSize);
    //管理员查看全部的用户的数量
    @Select("SELECT count(*) FROM user;")
    int listAllNum();

    //删除用户
    @Update("UPDATE user SET valid = 0 WHERE id = #{deleteId}")
    void deleteUser(Integer deleteId);
}
